package com.asset.dispoal.file.casedispose.strategy.command;

import com.alibaba.excel.EasyExcel;
import com.asset.dispoal.business.casedispose.domain.db.mapper.CasedisposeMapper;
import com.asset.dispoal.business.casedispose.domain.dto.CaseDisposeState;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import com.asset.dispoal.file.casedispose.dto.FileCaseConditionRequest;
import com.asset.dispoal.file.casedispose.dto.FileCaseExecutionRequest;
import com.asset.dispoal.file.casedispose.service.command.FileCaseExecutionCommand;
import com.asset.dispoal.file.casedispose.strategy.BaseFileUploadConditionStrategyServer;
import com.asset.dispoal.file.casedispose.strategy.dto.ExecutionExcelUploadDTO;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.strategy.StrategyHandlerParam;
import tech.mhuang.pacebox.core.strategy.StrategyHandlerResult;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.core.util.ObjectUtil;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class CaseExecutionStrategyServer implements BaseFileUploadConditionStrategyServer {

    @Autowired
    private CasedisposeMapper casedisposeMapper;

    @Autowired
    private FileCaseExecutionCommand fileCaseExecutionCommand;

    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public StrategyHandlerResult<Result> strategy(StrategyHandlerParam<FileCaseConditionRequest> request) {
        List<CaseDisposeState> caseStateList = casedisposeMapper.queryCaseStateByAppointId(request.getData().getAppointId());
        if (CollectionUtil.isEmpty(caseStateList)) {
            return new StrategyHandlerResult<>(false, Result.faild("批次包的案件不存在"));
        }
        caseStateList = caseStateList.stream().filter(caseState -> AgreementCaseSateMenu.JUDGMENT.getLabel().equals(caseState.getState())).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(caseStateList)) {
            return new StrategyHandlerResult<>(false, Result.faild("批次包下的案件没有待立案的案件"));
        }
        if (StringUtil.equals(request.getData().getType(), "2")) {
            List<ExecutionExcelUploadDTO> idCardList = EasyExcel.read(request.getData().getFile().getInputStream()).head(ExecutionExcelUploadDTO.class).sheet().doReadSync();
            if (CollectionUtil.isEmpty(idCardList)) {
                return new StrategyHandlerResult<>(false, Result.faild("身份证号不存在"));
            }
            Map<String, ExecutionExcelUploadDTO> executionExcelUploadDTOIdCardMap = idCardList.stream().collect(Collectors.toMap(ExecutionExcelUploadDTO::getIdCard, dto -> dto));
            Map<String, ExecutionExcelUploadDTO> executionExcelUploadDTOLitigationNumberMap = idCardList.stream().collect(Collectors.toMap(ExecutionExcelUploadDTO::getLitigationNumber, dto -> dto));
            boolean checked = Boolean.TRUE;
            for (CaseDisposeState caseDisposeState : caseStateList) {
                ExecutionExcelUploadDTO dto = executionExcelUploadDTOIdCardMap.get(caseDisposeState.getIdCard());
                if (ObjectUtil.isEmpty(dto)) {
                    dto = executionExcelUploadDTOLitigationNumberMap.get(caseDisposeState.getLitigationNumber());
                }
                if (ObjectUtil.isNotEmpty(dto)) {
                    checked = false;
                    FileCaseExecutionRequest fileCaseExecutionRequest = new FileCaseExecutionRequest();
                    fileCaseExecutionRequest.setId(caseDisposeState.getId());
                    fileCaseExecutionRequest.setExecutionNumber(dto.getExecutionNumber());
                    fileCaseExecutionRequest.setExecutionTime(dto.getExecutionTime());
                    fileCaseExecutionCommand.execute(fileCaseExecutionRequest);
                }
            }
            if (checked) {
                return new StrategyHandlerResult<>(false, Result.faild("找不到导入条件的身份证号"));
            }
        } else {
            for (CaseDisposeState caseDisposeState : caseStateList) {
                FileCaseExecutionRequest fileCaseExecutionRequest = new FileCaseExecutionRequest();
                fileCaseExecutionRequest.setId(caseDisposeState.getId());
                fileCaseExecutionCommand.execute(fileCaseExecutionRequest);
            }
        }
        return new StrategyHandlerResult<>(true, Result.ok());
    }

    @Override
    public List<String> event() {
        return Collections.singletonList("4");
    }
}
